package com.example.musicplayer.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.musicplayer.R;
import com.example.musicplayer.adapter.MusicItemAdapter;
import com.example.musicplayer.commonUtils.DisplayUtil;
import com.example.musicplayer.commonUtils.DrawUtil;
import com.example.musicplayer.commonUtils.ToastUtil;
import com.example.musicplayer.dialogTools.SortMusicDialog;
import com.example.musicplayer.musicClass.MusicInfo;
import com.example.musicplayer.musicClass.MusicMenu;
import com.example.musicplayer.musicTools.MusicListType;
import com.example.musicplayer.musicTools.MusicOp;
import com.example.musicplayer.musicTools.MusicSortType;
import com.example.musicplayer.service.MusicService;
import com.example.musicplayer.settings.MusicPlayerSettings;
import com.example.musicplayer.user.User;
import com.example.musicplayer.windowTools.WindowTools;

import java.util.ArrayList;
import java.util.List;

public class MusicMenuDisplayActivity extends BaseActivity {
    public static final int RESULT_CODE = 200;
    private MusicService.MusicBinder mMusicBinder;
    private ListView mListView;
    private MusicItemAdapter mAdapter;
    private final List<MusicInfo> mMusicList = new ArrayList<>();
    private View mNoMusicView;
    private View mToolbar;
    private View mControlView;
    private View mListHeaderView;
    private View mListFooterView;
    private View mHeaderInfoView;
    private View mHeaderControlView;
    private String mMusicMenuName;
    //对歌单是否调整
    private boolean mStateChanged = false;
    //是否更新整个页面
    private boolean mShouldUpdate = true;
    private boolean mToBottom = true;
    private Bitmap mBitmapAlbum;
    private int mChangeHeight;
    private int mToolbarHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_menu_display);
        if ((mMusicBinder = getMusicServiceBinder()) == null) {
            finish();
        }
        WindowTools.setStatusBarTransparent(getWindow());
        Intent intent = getIntent();
        mMusicMenuName = intent.getStringExtra("musicMenuName");
        if (mMusicMenuName == null) {
            mMusicMenuName = getString(R.string.description_null);
        }
        mToolbarHeight = (int) getResources().getDimension(R.dimen.ac_music_menu_display_toolbar_h);
        mChangeHeight = (int) getResources().getDimension(R.dimen.ac_music_menu_display_info_view_h) - mToolbarHeight;
        setEnableMusicListener(true);
        //show music playing info tab
        setEnableMusicTabView(true);
        registerMusicListener(mMusicBinder);
        checkAndUpdateMusicMenu();
        initView();
        addListener();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mShouldUpdate) {
            updateAll();
            mShouldUpdate = false;
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (mMusicBinder != null && hasFocus) {
            registerMusicListener(mMusicBinder);
        }
    }

    @Override
    public void onBackPressed() {
        finishActivity();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == MusicListManageActivity.RESULT_CODE && data != null) {
            if (data.getBooleanExtra("result", true)) {
                mStateChanged = true;
                //更新视图
                updateOtherView();
                updateMusicListStateChanged();
            }
        }
        if (resultCode == EditMusicMenuDetailsActivity.RESULT_CODE && data != null) {
            if (data.getBooleanExtra("result", true)) {
                mStateChanged = true;
                String newName = data.getStringExtra("musicMenuName");
                if (newName != null) {
                    mMusicMenuName = newName;
                }
                updateOtherView();
            }
        }
        if (resultCode == MusicSearchActivity.RESULT_CODE && data != null) {
            if (data.getBooleanExtra(MusicSearchActivity.NEED_LOCATE, false)) {
                int locateId = data.getIntExtra(MusicSearchActivity.LOCATE_ID, -1);
                for (int i = 0; i < mMusicList.size(); i++) {
                    if (mMusicList.get(i).getId() == locateId) {
                        locateMusicItem(i, 300);
                        break;
                    }
                }
            }
        }
    }

    //check data base and if need update menu music list
    private void checkAndUpdateMusicMenu() {
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        if (musicMenu != null) {
            mStateChanged = MusicInfo.checkAndUpdateMusicMenu(musicMenu);
        }
    }

    private void initView() {
        mListView = findViewById(R.id.list_view);
        mNoMusicView = findViewById(R.id.no_music_view);
        mToolbar = findViewById(R.id.toolbar);
        mControlView = generateControlView();
        mListHeaderView = generateListHeaderView();
        mListFooterView = generateListFooterView();
        ((LinearLayout) findViewById(R.id.container)).addView(mControlView);
        DisplayUtil.setRoundRectOutline(mHeaderInfoView.findViewById(R.id.image_album), 0, 0,
                (int) getResources().getDimension(R.dimen.ac_music_menu_display_image_size),
                (int) getResources().getDimension(R.dimen.ac_music_menu_display_image_size),
                (int) getResources().getDimension(R.dimen.big_image_radius));
        DisplayUtil.setRoundOutline(mHeaderInfoView.findViewById(R.id.user_photo), 0, 0,
                (int) getResources().getDimension(R.dimen.user_photo_size_small),
                (int) getResources().getDimension(R.dimen.user_photo_size_small));
    }

    @SuppressLint("InflateParams")
    private View generateListHeaderView() {
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        mHeaderInfoView = LayoutInflater.from(this).inflate(R.layout.ac_music_menu_display_info_view, null);
        mHeaderInfoView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                (int) getResources().getDimension(R.dimen.ac_music_menu_display_info_view_h)));
        mHeaderControlView = generateControlView();
        linearLayout.addView(mHeaderInfoView);
        linearLayout.addView(mHeaderControlView);
        return linearLayout;
    }

    @SuppressLint("InflateParams")
    private View generateListFooterView() {
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                (int) getResources().getDimension(R.dimen.normal_footer_view_height)));
        return linearLayout;
    }

    @SuppressLint("InflateParams")
    private View generateControlView() {
        View view = LayoutInflater.from(this).inflate(R.layout.ac_music_menu_display_control_view, null);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                (int) getResources().getDimension(R.dimen.ac_music_menu_display_control_view_h)));
        view.findViewById(R.id.play).setOnClickListener(v -> doClickPlay(0));
        view.findViewById(R.id.play_info).setOnClickListener(v -> doClickPlay(0));
        view.findViewById(R.id.manage).setOnClickListener(v -> doClickManage());
        view.findViewById(R.id.sort).setOnClickListener(v -> doClickSort());
        view.findViewById(R.id.add).setOnClickListener(v -> doClickAdd());
        view.findViewById(R.id.locate).setOnClickListener(v -> doClickLocate());
        return view;
    }

    private void addListener() {
        mToolbar.findViewById(R.id.finish).setOnClickListener(v -> finishActivity());
        mToolbar.findViewById(R.id.ac_menu).setOnClickListener(v -> doClickMenu());
        mToolbar.findViewById(R.id.search_music).setOnClickListener(v -> doClickSearchMusic());
        mNoMusicView.findViewById(R.id.button_add_music).setOnClickListener(v -> doClickAdd());
        mHeaderInfoView.findViewById(R.id.collect).setOnClickListener(v -> doClickCollect());
        mHeaderInfoView.findViewById(R.id.collect_text).setOnClickListener(v -> doClickCollect());
        mHeaderInfoView.findViewById(R.id.comment).setOnClickListener(v -> doClickComment());
        mHeaderInfoView.findViewById(R.id.comment_text).setOnClickListener(v -> doClickComment());
        mHeaderInfoView.findViewById(R.id.share).setOnClickListener(v -> doClickShare());
        mHeaderInfoView.findViewById(R.id.share_text).setOnClickListener(v -> doClickShare());
        mHeaderInfoView.findViewById(R.id.edit_introduction).setOnClickListener(v -> doClickEditIntroduction());
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                updateToolbar();
            }
        });
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            //has a header view
            doClickPlay(position - 1);
        });
        mListView.setOnItemLongClickListener((parent, view, position, id) -> {
            doClickManage();
            return true;
        });
    }

    private void updateAll() {
        updateOtherView();
        updateListView();
        updateUserInfo();
    }

    //update music menu info, not music list view
    private void updateOtherView() {
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        int musicCounts = musicMenu == null ? 0 : musicMenu.getCounts();
        if (musicCounts == 0) {
            mNoMusicView.setVisibility(View.VISIBLE);
            mControlView.setVisibility(View.GONE);
            mHeaderControlView.setVisibility(View.GONE);
        } else {
            mNoMusicView.setVisibility(View.GONE);
            mHeaderControlView.setVisibility(View.VISIBLE);
            //显示歌单音乐数量
            String text = getResources().getString(R.string.play_all_music) + "(" + musicCounts + ")";
            ((TextView) mControlView.findViewById(R.id.play_info)).setText(text);
            ((TextView) mHeaderControlView.findViewById(R.id.play_info)).setText(text);
        }
        if (musicCounts >= MusicPlayerSettings.getLeastMusicCountsUseLocation()) {
            mControlView.findViewById(R.id.locate).setVisibility(View.VISIBLE);
            mHeaderControlView.findViewById(R.id.locate).setVisibility(View.VISIBLE);
        } else {
            mControlView.findViewById(R.id.locate).setVisibility(View.GONE);
            mHeaderControlView.findViewById(R.id.locate).setVisibility(View.GONE);
        }
        ((TextView) mHeaderInfoView.findViewById(R.id.music_menu_name)).setText(mMusicMenuName);
        setImageBitmap();
        setToolbarBackground();
        updateMusicMenuIntroduction();
        updateToolbar();
        updateMusicMenuListenCounts();
    }

    //after add or delete or change order, update music list view
    private void updateListView() {
        List<Integer> idList = MusicOp.sort(mMusicMenuName);
        mMusicList.clear();
        if (idList == null) {
            idList = new ArrayList<>();
        }
        for (int i = 0; i < idList.size(); i++) {
            MusicInfo temp = MusicInfo.getMusicInfoById(idList.get(i));
            if (temp != null) {
                mMusicList.add(temp);
            }
        }
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        int displayMode = MusicItemAdapter.MODE_MUSIC_MENU_NORMAL;
        if (musicMenu != null && musicMenu.getMusicSortType() == MusicSortType.SORT_LISTEN_COUNTS) {
            displayMode = MusicItemAdapter.MODE_MUSIC_MENU_LISTEN_INFO;
        }
        mAdapter = new MusicItemAdapter(this, mMusicList, mMusicBinder, displayMode);
        mListView.setAdapter(mAdapter);
        //if need add footer and header view
        mListView.removeHeaderView(mListHeaderView);
        mListView.removeFooterView(mListFooterView);
        mListView.addHeaderView(mListHeaderView, null, false);
        mListView.addFooterView(mListFooterView, null, false);
        mAdapter.setOnMoreFeaturesListener(new MusicItemAdapter.OnMoreFeaturesListener() {
            @Override
            public void onExpandMoreFeatures(MusicInfo musicInfo) {
                ToastUtil.makeToast("展开更多功能");
            }

            @Override
            public void onRequestMusicVideo(MusicInfo musicInfo) {
                ToastUtil.makeToast("播放歌曲MV");
            }
        });
    }

    private void updateMusicListStateChanged() {
        List<Integer> idList = MusicOp.sort(mMusicMenuName);
        mMusicList.clear();
        if (idList == null) {
            idList = new ArrayList<>();
        }
        for (int i = 0; i < idList.size(); i++) {
            MusicInfo temp = MusicInfo.getMusicInfoById(idList.get(i));
            if (temp != null) {
                mMusicList.add(temp);
            }
        }
        if (mAdapter != null) {
            mAdapter.setMusicList(mMusicList);
        }
    }

    private void updateUserInfo() {
        //set photo for current user
        ImageView userImageView = mHeaderInfoView.findViewById(R.id.user_photo);
        if (User.getInstance() == null) {
            userImageView.setImageBitmap(mBitmapAlbum);
        } else {
            userImageView.setImageBitmap(User.getInstance().getUserPhoto());
            ((TextView) mHeaderInfoView.findViewById(R.id.user_name)).setText(User.getInstance().getUserName());
        }
    }

    //设置歌单图片
    private void setImageBitmap() {
        //获取歌单封面图
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        if (musicMenu == null) {
            mBitmapAlbum = DrawUtil.getDefaultMusicMenuBitmap();
        } else {
            mBitmapAlbum = musicMenu.getBitmap();
        }
        //set bitmap for current music menu
        Bitmap bitmap = DrawUtil.copyBitmap(mBitmapAlbum);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.parseColor("#44000000"));
        ((ImageView) mHeaderInfoView.findViewById(R.id.image_album)).setImageBitmap(bitmap);
    }

    private void setToolbarBackground() {
        Bitmap bitmap = DrawUtil.getColorBitmap(Color.WHITE);
        int startColor = DrawUtil.getMainColorOfPicture(mBitmapAlbum, DrawUtil.DARK_VIBRANT);
        int endColor = DrawUtil.getMainColorOfPicture(mBitmapAlbum, DrawUtil.VIBRANT);
        bitmap = DrawUtil.getSlantedGradientBitmap(bitmap, startColor, endColor);
        mToolbar.setBackground(new BitmapDrawable(getResources(), bitmap));
        mHeaderInfoView.setBackground(new BitmapDrawable(getResources(), bitmap));
    }

    //更新简介
    private void updateMusicMenuIntroduction() {
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        TextView introduction = mHeaderInfoView.findViewById(R.id.introduction_text);
        if (musicMenu != null) {
            String text = musicMenu.getIntroduction();
            if (text != null) {
                introduction.setText(text);
            }
        }
        introduction.setTextColor(DrawUtil.getBodyTextColorOfPicture(mBitmapAlbum, DrawUtil.VIBRANT));
    }

    //更新toolbar显示
    private void updateToolbar() {
        int[] location = new int[2];
        if (mHeaderControlView != null) {
            mHeaderControlView.getLocationOnScreen(location);
        }
        if (location[1] <= mToolbarHeight && mHeaderControlView.getVisibility() == View.VISIBLE) {
            mControlView.setVisibility(View.VISIBLE);
        } else {
            mControlView.setVisibility(View.GONE);
        }
        float distance = mChangeHeight + mToolbarHeight - location[1];
        //toolbar的透明度
        int alpha = (int) (distance * 1.1f / (float) mChangeHeight * 255);
        //toolbar文字的透明度
        float alphaText = distance * 2 / (float) mChangeHeight;
        alpha = alpha < 0 ? 0 : Math.min(alpha, 255);
        alphaText = alphaText < 0 ? 0 : Math.min(alphaText, 2);
        if (mToolbar.getBackground() != null) {
            mToolbar.getBackground().setAlpha(alpha);
        }
        TextView toolbarText = mToolbar.findViewById(R.id.ac_title);
        if (alphaText <= 1.0f) {
            toolbarText.setText(R.string.music_menu);
            toolbarText.setAlpha(1 - alphaText);
        } else {
            toolbarText.setText(mMusicMenuName);
            toolbarText.setAlpha(alphaText - 1);
        }
    }

    //更新播放次数
    private void updateMusicMenuListenCounts() {
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        if (musicMenu != null) {
            int listenCounts = musicMenu.getListenCounts();
            ((TextView) mHeaderInfoView.findViewById(R.id.music_menu_listen_counts)).setText(String.valueOf(listenCounts));
        }
    }

    private void updateLocateImage() {
        if (mToBottom) {
            ((ImageView) mControlView.findViewById(R.id.locate)).setBackgroundResource(R.drawable.icon_to_bottom);
            ((ImageView) mHeaderControlView.findViewById(R.id.locate)).setBackgroundResource(R.drawable.icon_to_bottom);
        } else {
            ((ImageView) mControlView.findViewById(R.id.locate)).setBackgroundResource(R.drawable.icon_to_top);
            ((ImageView) mHeaderControlView.findViewById(R.id.locate)).setBackgroundResource(R.drawable.icon_to_top);
        }
    }

    private void doClickPlay(int index) {
        if (mMusicList.size() > 0) {
            if (index < 0 || index >= mMusicList.size()) {
                index = 0;
            }
            if (mMusicBinder != null && mMusicList.get(index) != null) {
                if (mMusicList.get(index).getId() != mMusicBinder.getId() ||
                        mMusicBinder.getMusicListType() != MusicListType.TYPE_MENU) {
                    mMusicBinder.start(MusicListType.TYPE_MENU, MusicInfo.getMusicMenuByName(mMusicMenuName)
                            , mMusicList.get(index));
                } else if (!mMusicBinder.isPlaying()) {
                    mMusicBinder.start();
                }
                if (MusicPlayerSettings.isAutoIntoPlayScreen()) {
                    jumpToMusicPlayActivity();
                }
            }
        }
    }

    private void doClickManage() {
        Intent intent = new Intent(this, MusicListManageActivity.class);
        intent.putExtra("musicListType", MusicListType.TYPE_MENU);
        intent.putExtra("musicMenuName", mMusicMenuName);
        jumpToActivity(intent);
    }

    private void doClickSort() {
        final MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        //获取歌单的当前排序类型
        MusicSortType type = musicMenu == null ? MusicSortType.SORT_DEFAULT : musicMenu.getMusicSortType();
        //构造排序选择对话框
        SortMusicDialog sortMusicDialog = new SortMusicDialog(this, type);
        final Dialog dialog = sortMusicDialog.getDialog();
        //监听事件
        sortMusicDialog.getCancel().setOnClickListener(v -> dialog.dismiss());
        sortMusicDialog.getSortDefault().setOnClickListener(v -> {
            if (musicMenu != null) {
                musicMenu.setMusicSortType(MusicSortType.SORT_DEFAULT);
            }
            if (mAdapter != null) {
                mAdapter.setMode(MusicItemAdapter.MODE_MUSIC_MENU_NORMAL);
            }
            updateMusicListStateChanged();
            dialog.dismiss();
        });
        sortMusicDialog.getSortDefaultReverse().setOnClickListener(v -> {
            if (musicMenu != null) {
                musicMenu.setMusicSortType(MusicSortType.SORT_DEFAULT_REVERSE);
            }
            if (mAdapter != null) {
                mAdapter.setMode(MusicItemAdapter.MODE_MUSIC_MENU_NORMAL);
            }
            updateMusicListStateChanged();
            dialog.dismiss();
        });
        sortMusicDialog.getSortTitle().setOnClickListener(v -> {
            if (musicMenu != null) {
                musicMenu.setMusicSortType(MusicSortType.SORT_TITLE);
            }
            if (mAdapter != null) {
                mAdapter.setMode(MusicItemAdapter.MODE_MUSIC_MENU_NORMAL);
            }
            updateMusicListStateChanged();
            dialog.dismiss();
        });
        sortMusicDialog.getSortArtist().setOnClickListener(v -> {
            if (musicMenu != null) {
                musicMenu.setMusicSortType(MusicSortType.SORT_ARTIST);
            }
            if (mAdapter != null) {
                mAdapter.setMode(MusicItemAdapter.MODE_MUSIC_MENU_NORMAL);
            }
            updateMusicListStateChanged();
            dialog.dismiss();
        });
        sortMusicDialog.getSortListenCounts().setOnClickListener(v -> {
            if (musicMenu != null) {
                musicMenu.setMusicSortType(MusicSortType.SORT_LISTEN_COUNTS);
            }
            if (mAdapter != null) {
                mAdapter.setMode(MusicItemAdapter.MODE_MUSIC_MENU_LISTEN_INFO);
            }
            updateMusicListStateChanged();
            dialog.dismiss();
        });
        dialog.show();
    }

    private void doClickAdd() {
        Intent intent = new Intent(this, MusicListManageActivity.class);
        intent.putExtra("musicListType", MusicListType.TYPE_LOCAL);
        intent.putExtra("musicMenuName", mMusicMenuName);
        jumpToActivity(intent);
    }

    private void doClickLocate() {
        int firstPosition = mListView.getFirstVisiblePosition();
        int lastPosition = mListView.getLastVisiblePosition();
        int targetPosition = -1;
        for (int i = 0; i < mMusicList.size(); i++) {
            if (mMusicList.get(i).getId() == mMusicBinder.getId()) {
                targetPosition = i;
                break;
            }
        }
        if (targetPosition >= firstPosition && targetPosition <= lastPosition) {
            if (mToBottom) {
                locateMusicItem(mMusicList.size() - 1, 0);
                mToBottom = false;
            } else {
                locateMusicItem(0, 0);
                mToBottom = true;
            }
        } else {
            locateMusicItem(targetPosition, 300);
        }
        updateLocateImage();
    }

    private void doClickMenu() {

    }

    private void doClickSearchMusic() {
        Intent intent = new Intent(this, MusicSearchActivity.class);
        intent.putExtra(MusicSearchActivity.TYPE, MusicListType.TYPE_MENU.toString());
        intent.putExtra(MusicSearchActivity.MUSIC_MENU, mMusicMenuName);
        jumpToActivity(intent);
    }

    private void doClickEditIntroduction() {
        Intent intent = new Intent(this, EditMusicMenuDetailsActivity.class);
        intent.putExtra("musicMenuName", mMusicMenuName);
        jumpToActivity(intent);
    }

    private void doClickCollect() {

    }

    private void doClickComment() {

    }

    private void doClickShare() {

    }

    private void locateMusicItem(int position, int offset) {
        if (mListView != null) {
            mListView.smoothScrollToPositionFromTop(position, offset, 300);
        }
    }

    //跳转到其它activity
    private void jumpToActivity(Intent intent) {
        startActivityForResult(intent, 1);
        setPendingTransition(R.anim.activity_vertical_enter, R.anim.fake_anim);
    }

    //结束activity
    private void finishActivity() {
        Intent intent = new Intent();
        intent.putExtra("result", mStateChanged);
        this.setResult(RESULT_CODE, intent);
        finish();
        setPendingTransition(R.anim.slow_activity_enter, R.anim.normal_activity_exit);
    }

    @Override
    protected void onMusicPrepared(MusicInfo musicInfo) {
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onMusicEmpty(MusicInfo musicInfo) {
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }
}